package com.boot.controller.user;

import com.boot.common.message.AjaxResult;
import com.boot.common.message.RequestPageHead;
import com.boot.common.message.ResultPage;
import com.alibaba.fastjson.JSON;
import com.boot.common.message.BaseService;
import com.boot.common.utils.PreconditionsUtils;
import com.boot.service.user.IUserService;
import com.boot.entity.user.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


/**
 * @author chengjiye
 * @description 用户表控制器
 * @date 2020-10-03 11:43
 */
@Api(tags = "用户表接口")
@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "获取用户表列表")
    @PostMapping("list")
    public ResultPage<List<UserEntity>> list(@RequestBody RequestPageHead req) {
        List<String> list=null;
        PreconditionsUtils.checkNotNull(list,"list不能为空");
        log.debug("UserController::list接口开始执行... " + "参数是：" + JSON.toJSONString(req));
        return BaseService.packPageResult(userService.queryUserEntityList(req));

    }

    @ApiOperation(value = "按userId获取用户表详情")
    @PostMapping("detail")
    public AjaxResult<UserEntity> detail(@RequestBody UserEntity reqDetail) {
        log.debug("UserController::detail接口开始执行... " + "参数是：" + JSON.toJSONString(reqDetail));
        return AjaxResult.success(userService.queryUserEntityById(reqDetail));
    }

    @ApiOperation(value = "新增用户表")
    @PostMapping("add")
    public AjaxResult add(@RequestBody UserEntity reqAdd) {
        log.debug("UserController::add接口开始执行.. " + "参数是：" + JSON.toJSONString(reqAdd));
        userService.insertUserEntity(reqAdd);
        return AjaxResult.success();
    }

    @ApiOperation(value = "按id删除用户表")
    @PostMapping("delete")
    public AjaxResult delete(@RequestBody UserEntity reqDelete) {
        log.debug("UserController::delete接口开始执行... " + "参数是：" + JSON.toJSONString(reqDelete));
        userService.deleteUserEntityById(reqDelete);
        return AjaxResult.success();
    }

    @ApiOperation(value = "按id修改用户表")
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody UserEntity reqEdit) {
        log.debug("UserController::edit接口开始执行... " + "参数是：" + JSON.toJSONString(reqEdit));
        userService.updateUserEntityById(reqEdit);
        return AjaxResult.success();
    }

}
